﻿using System;
using System.IO;

namespace DominionWar
{
    class BaseShip
    {
        // Ship damage reporting levels
        protected const int MAXIMUM_PERCENTAGE = 100;

        protected enum Damage
        {
            UNDAMAGED = 100,
            LIGHTLY_DAMAGED = 90,
            MODERATELY_DAMAGED = 70,
            HEAVILY_DAMAGED = 40,
            VERY_HEAVILY_DAMAGED = 0
        }

        // Ship attributes
        protected String Name;
        protected int ShieldStrength;
        protected int CurrentShieldStrength;
        protected int RegenerationRate;
        protected int HullStrength;
        protected int CurrentHullStrength;
        protected int WeaponBaseDamage;
        protected int WeaponRandomDamage;

        // Ship state flags
        protected bool Destroyed = false;
        protected bool BeenHit = false;

        public BaseShip()
        {

        }

        public String GetName()
        {
            return Name;
        }

        public int GetWeaponBaseDamage()
        {
            return WeaponBaseDamage;
        }

        public int GetWeaponRandomDamage()
        {
            return WeaponRandomDamage;
        }

        public void Hit(int damage)
        {
            this.BeenHit = true;
            int surplusDamage = this.HitShield(damage);

            if (surplusDamage > 0)
                this.HitHull(surplusDamage);
        }

        public void RechargeShield()
        {
            if (this.BeenHit == false)
            {
                this.CurrentShieldStrength += this.RegenerationRate;
                if (this.CurrentShieldStrength > this.ShieldStrength)
                    this.CurrentShieldStrength = this.ShieldStrength;
            }
            else
                this.BeenHit = false; // Reset the flag for the next round of attacks
        }

        public String DamageLevel()
        {
            int percentageUndamaged = (int)this.CurrentHullStrength * MAXIMUM_PERCENTAGE / this.HullStrength;

            if (percentageUndamaged >= (int)Damage.UNDAMAGED)
                return "undamaged";
            else if (percentageUndamaged >= (int)Damage.LIGHTLY_DAMAGED)
                return "lightly damaged";
            else if (percentageUndamaged >= (int)Damage.MODERATELY_DAMAGED)
                return "moderately damaged";
            else if (percentageUndamaged >= (int)Damage.HEAVILY_DAMAGED)
                return "heavily damaged";
            else
                return "very heavily damaged";
        }

        public bool hasBeenHit()
        {
            return this.BeenHit;
        }

        public bool isDestroyed()
        {
            return this.Destroyed;
        }

        protected int HitShield(int damage)
        {
            // Damage the shields first
            this.CurrentShieldStrength -= damage;

            // Check for excess shield damage; if any return that surplus amount
            int surplusDamage = 0;
            if (this.CurrentShieldStrength < 0)
            {
                surplusDamage = Math.Abs(this.CurrentShieldStrength); // Any amount of shields < 0 should be applied to the hull instead
                this.CurrentShieldStrength = 0;
            }
            return surplusDamage;
        }

        protected void HitHull(int damage)
        {
            CurrentHullStrength -= damage;

            if (CurrentHullStrength <= 0)
                Destroyed = true;
        }
    }
}